package com.gezhi.stuserver.configure;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Configuration 的作用：指定该类是一个Spring容器的配置类
 * 指定properties配置文件中内容的前缀
 * @ConfigurationProperties(prefix="spring.alibaba.datasource")
 * 指定与该类发生关系的properties配置文件的路径在哪儿
 * @PropertySource(value= {"classpath:druid.properties"})
 *
 *
 *
 * @author 格智学院
 */
@Configuration
public class DruidDataSourceConfigure {

    @Resource
    private Druid druid;

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", druid.getLoginUsername());
        reg.addInitParameter("loginPassword", druid.getLoginPassword());
        reg.addInitParameter("logSlowSql", druid.getLogSlowSql());
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        return filterRegistrationBean;
    }

    /**
     * @Bean 用来产生Spring组件
     * dataSource方法名，就是组件在Spring容器中的ID
     * @return
     */
    @Bean
    public DataSource dataSource(){
        //产生一个阿里巴巴的数据源连接池
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(druid.getUrl());
        dataSource.setUsername(druid.getUserName());
        dataSource.setPassword(druid.getPassword());
        dataSource.setInitialSize(druid.getInitialSize());
        dataSource.setMinIdle(druid.getMinIdle());
        dataSource.setMaxActive(druid.getMaxActive());
        dataSource.setMaxWait(druid.getMaxWait());
        dataSource.setTimeBetweenEvictionRunsMillis(druid.getTimeBetweenEvictionRunsMillis());
        dataSource.setMinEvictableIdleTimeMillis(druid.getMinEvictableIdleTimeMillis());
        dataSource.setValidationQuery(druid.getValidationQuery());
        dataSource.setTestWhileIdle(druid.isTestWhileIdle());
        dataSource.setTestOnBorrow(druid.isTestOnBorrow());
        dataSource.setTestOnReturn(druid.isTestOnReturn());

        try {
            dataSource.setFilters(druid.getFilters());
        } catch (SQLException e) {
           e.printStackTrace();
        }
        return dataSource;
    }

}
